//-------------------------------------------------------------------------------------------------
// <copyright file="Configure.cs" company="OfficeClip LLC">
// Copyright (c) OfficeClip LLC.  All rights reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------

namespace OfficeClip.OpenSource.StyleFix
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    public partial class Configure : Form
    {
        public Configure()
        {
            this.InitializeComponent();
            this.txtCompanyName.Text = Properties.Settings.Default.CompanyName;
            this.SelectedSA = Properties.Settings.Default.SelectedSA;
        }

        private void chkSARules_CheckedChanged(object sender, EventArgs e)
        {
            this.CheckListBox(this.SAListBox, this.chkSARules.Checked);
        }

        private string SelectedSA
        {
            get
            {
                StringBuilder sb = new StringBuilder();
               for (int i = 0; i < this.SAListBox.Items.Count; i++)
               {
                   if (this.SAListBox.GetItemChecked(i))
                   {
                       if (sb.Length > 0)
                       {
                           sb.Append(",");
                       }

                       sb.Append(((string) SAListBox.Items[i]).Split("-".ToCharArray())[0].Trim());
                   }
               }

                return sb.ToString();
            }

            set
            {
                // Sets the checkboxes from the values
                string[] astr = ((string) value).Split(",".ToCharArray());

                // SAListBox.Items.Clear();
                this.CheckListBox(this.SAListBox, false);
                string saValue1;
                for (int i = 0; i < this.SAListBox.Items.Count; i++)
                {
                    saValue1 = ((string) SAListBox.Items[i]).Split("-".ToCharArray())[0].Trim();
                    if (((string) value).Contains(saValue1))
                    {
                        this.SAListBox.SetItemChecked(i, true);
                    }
                }
            }
        }

        private void CheckListBox(CheckedListBox checkListBox, bool isChecked)
        {
            for (int i = 0; i < checkListBox.Items.Count; i++)
            {
                checkListBox.SetItemChecked(i, isChecked);
            }
        }

        public bool IsSAAllowed(string saString)
        {
            bool isSelected = false;
            for (int i = 0; i < this.SAListBox.Items.Count; i++)
            {
                if (
                    (((string)SAListBox.Items[i]).IndexOf(saString) > -1) &&
                    this.SAListBox.GetItemChecked(i))
                {
                    isSelected = true;
                    break;
                }
            }

            return isSelected;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.CompanyName = this.txtCompanyName.Text;
            Properties.Settings.Default.SelectedSA = this.SelectedSA;
            Properties.Settings.Default.Save();
            this.Close();
        }
    }
}
